#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

#include "ipc_api.h" 

typedef struct {
	int			running;
}shareMemServer_s;
shareMemServer_s	shareMemServer = {1};
shareMemServer_s	*pShareMemServer = &shareMemServer;

void signalRountine(int signNo)
{
	switch(signNo)
	{
		case SIGINT://ctrl+c
			pShareMemServer->running = 0;
			break;
		case SIGTERM://kill
			pShareMemServer->running = 0;
			break;
	}
}

int main(int argc,char* argv[])   
{
	ipcRet_e ret = ipcSuccess;
	ipcLog_s log = {IPC_LOG_DEBUG,IPC_LOG_2_STDERR};	  
	int create	 = TRUE;
	ipcLockContext_s context ={getpid(),(char *)"server"};

		
	signal(SIGINT,signalRountine);
	signal(SIGTERM,signalRountine);

	ret = ipcLogInit(&log);
	ipcLogCheckRetThenReturn(ret);

	ret = ipcShareMemoryInit(create);
	ipcLogCheckRetThenReturn(ret);	
	
	ret = ipcLockInit(create);
	ipcLogCheckRetThenReturn(ret);

	ret = ipcLock(&context);
	if(ipcSuccess == ret)
	{
		pIPCHandle->yourData[0] = 's';
		ipcUnlock(&context);
	}

	while(pShareMemServer->running)
	{
		if(0 == pIPCHandle->yourData[1])
		{
			sleep(1);
		}
		else
		{	
			ipcLogDebug("server read yourData[1] %c \n",pIPCHandle->yourData[1]);
			break;
		}
	}

	ipcLockDeinit(create);
	ipcShareMemoryDeinit(create);
	ipcLogDeinit();
	
	return 0;
}
